<?php

namespace app\admin\model;

use think\Model;


class Goods extends Model
{

    

    

    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'out_time_text',
        'type_text',
        'status_text',
        'is_postage_text',
        'is_top_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '-1' => __('Status -1')];
    }

    public function getIsPostageList()
    {
        return ['1' => __('Is_postage 1'), '-1' => __('Is_postage -1')];
    }

    public function getIsTopList()
    {
        return ['1' => __('Is_top 1'), '-1' => __('Is_top -1')];
    }


    public function getOutTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['out_time']) ? $data['out_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsPostageTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_postage']) ? $data['is_postage'] : '');
        $list = $this->getIsPostageList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsTopTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_top']) ? $data['is_top'] : '');
        $list = $this->getIsTopList();
        return isset($list[$value]) ? $list[$value] : '';
    }

//    protected function setOutTimeAttr($value)
//    {
//        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime(1111) : $value);
//    }


    public function goodsskuvalue()
    {
        return $this->belongsTo('GoodsSkuValue', 'value_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function goodscate()
    {
        return $this->belongsTo('GoodsCate', 'goods_cate_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
